/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.validator;

import com.minierp.bean.AdjustBillBean;
import com.minierp.bean.AdjustBillItemBean;
import com.minierp.dao.AdjustBillDao;
import com.minierp.model.AdjustBill;
import com.minierp.model.AdjustBillItem;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class AdjustBillValidator implements Validator {
	private AdjustBillDao adjustBillDao;

	public boolean supports(Class clazz) {
		return AdjustBillBean.class.isAssignableFrom(clazz);
	}

	public void validate(Object obj, Errors errors) {
		AdjustBillBean bean = (AdjustBillBean) obj;
		AdjustBill form = bean.getAdjustBill();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		boolean hasError = false;
		if (form.getTallySetId() == 0L) {
			hasError = true;
			errors.rejectValue("adjustBill.tallySetId", "", "请选择『当前开单帐套』");
		}

		if (!hasError
				&& (bean.getCodeRule().getGenerateRule().equals("MANUAL") || bean
						.getOldId() != 0L)) {
			if (form.getBillCode() != null
					&& !form.getBillCode().trim().equals("")) {
				AdjustBill total = this.adjustBillDao.get(form.getBillCode());
				if (total != null && total.getId() != form.getId()) {
					errors.rejectValue("adjustBill.billCode", "", "『编号』已经存在");
					hasError = true;
				}
			} else {
				errors.rejectValue("adjustBill.billCode", "", "请输入『编号』");
				hasError = true;
			}
		}

		if (!hasError) {
			if (form.getClientId() == 0L) {
				errors.rejectValue("adjustBill.clientId", "", "请选择『调账单位』");
				hasError = true;
			} else {
				try {
					form.setAdjustDate(sf.parse(bean.getAdjustDateStr()));
				} catch (Exception arg12) {
					hasError = true;
					errors.rejectValue("adjustDateStr", "",
							"『调账日期』填写错误，请按以下格式输入：2008-01-01");
				}
			}
		}

		if (!hasError) {
			if (form.getManagerId() == 0L) {
				errors.rejectValue("adjustBill.managerId", "", "请选择『经办人员』");
				hasError = true;
			} else if (form.getDepartmentId() == 0L) {
				errors.rejectValue("adjustBill.departmentId", "", "请选择『部门』");
				hasError = true;
			} else {
				try {
					form.setCost(new BigDecimal(bean.getCostStr()));
				} catch (Exception arg11) {
					hasError = true;
					errors.rejectValue("costStr", "", "『调账金额』只能输入数字");
				}
			}
		}

		if (!hasError && form.getCost().compareTo(new BigDecimal("0")) == 0) {
			hasError = true;
			errors.rejectValue("costStr", "", "『调账金额』不能为0");
		}

		if (!hasError && form.getDescription() != null
				&& form.getDescription().length() >= 2000) {
			hasError = true;
			errors.rejectValue("adjustBill.description", "", "『摘要』不能超过2000个字符");
		}

		if (!hasError && form.getMark() != null
				&& form.getMark().length() >= 250) {
			hasError = true;
			errors.rejectValue("adjustBill.mark", "", "『备注』不能超过250个字符");
		}

		if (!hasError) {
			BigDecimal arg14 = new BigDecimal(0);

			for (int i = 0; i < bean.getItems().size(); ++i) {
				AdjustBillItemBean cb = (AdjustBillItemBean) bean.getItems()
						.get(i);
				if (!cb.getStatus().equals("DELETE")) {
					AdjustBillItem c = cb.getAdjustBillItem();

					try {
						c.setThisCost(new BigDecimal(cb.getThisCostStr()));
						if (c.getThisCost().compareTo(new BigDecimal(0)) == -1) {
							hasError = true;
							errors.rejectValue("items", "", "『本次调账金额』不能为负数");
							break;
						}

						arg14 = arg14.add(c.getThisCost());
					} catch (Exception arg13) {
						hasError = true;
						errors.rejectValue("items", "", "『本次调账金额』只能输入数字");
						break;
					}
				}
			}

			if (!hasError && arg14.compareTo(form.getCost()) != 0) {
				hasError = true;
				errors.rejectValue("items", "", "『调账金额』与『结算金额总数』不符");
			}
		}

	}

	public AdjustBillDao getAdjustBillDao() {
		return this.adjustBillDao;
	}

	public void setAdjustBillDao(AdjustBillDao adjustBillDao) {
		this.adjustBillDao = adjustBillDao;
	}
}